using Microsoft.AspNetCore.Mvc.ModelBinding;
using StudentVideo.Common.Models;
using StudentVideo.Extensions;

namespace StudentVideo.Binders;

public class CurrentUserBinder: IModelBinder {
   public Task BindModelAsync(ModelBindingContext bindingContext) {
      ArgumentNullException.ThrowIfNull(bindingContext);

      var currentUser = bindingContext.HttpContext.GetUserData();

      bindingContext.Result = ModelBindingResult.Success(currentUser);
      return Task.CompletedTask;
   }
}

public class CurrentUserBinderProvider: IModelBinderProvider {
   public IModelBinder? GetBinder(ModelBinderProviderContext context) {
      if (context.Metadata.ModelType == typeof(CurrentUser))
         return new CurrentUserBinder();

      return null;
   }
}
